//
//  FindRecomentViewController.m
//  AriseSamePro
//
//  Created by 包日守 on 15/10/22.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "FindRecomentViewController.h"

#import "UIViewAdditions.h"

#import "ARGlobalInterface.h"

#import "ZJModelTool.h"


#import "ARFindHeadModel.h"


#import "ChannelCell.h"

#import "ChannelTableViewController.h"

#define TABBAR_HEIGHT 49
#define NAVAGATEBAR_HEIGHT 44
#define STARBAR_HEIGHT 20
#define HEADVIEW_HEIGHT 200
#define FOOTVIEW_WIDTH 30
#define FOOTVIEW_HEIGHT 60
#define CELL_HEIGHT 60
#define TABLEVIEW_HEADER_HEIGHT 40
#define TABLEVIEW_FOOTER_HEIGHT 1

@interface FindRecomentViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,ARFindHeadViewDelegate>
@property (nonatomic,weak) UITableView *tableView;


@property (nonatomic) ARFindHeadModel *headModel;
@property (nonatomic) NSArray *channelsModels;
@property (nonatomic,weak) UIImageView *footV;



@end

@implementation FindRecomentViewController

- (UITableView *)tableView
{
    if (_tableView == nil) {
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - TABBAR_HEIGHT - NAVAGATEBAR_HEIGHT - STARBAR_HEIGHT) style:UITableViewStyleGrouped];
        
        [self.view addSubview:tableView];
        _tableView = tableView;
        
        tableView.delegate = self;
        tableView.dataSource = self;
        
        
        [self.tableView registerNib:[UINib nibWithNibName:@"ChannelCell" bundle:nil] forCellReuseIdentifier:@"ChannelCellID"];
        
        
    }
    return _tableView;

}

- (ARFindHeadModel *)headModel
{
    if (_headModel == nil) {
        _headModel = [[ARFindHeadModel alloc] init];
    }
    return _headModel;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    // NSLog(@"%@",[ARGlobalInterface findRecomentCacheURL]);
     
     self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, HEADVIEW_HEIGHT)];
     
     ARFindHeardView *headView = [[ARFindHeardView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, HEADVIEW_HEIGHT)];
     [self.view addSubview:headView];
     
     self.headView = headView;
     headView.backgroundColor = [UIColor blueColor];
    
    self.headView.headViewDelegate = self;
    
    
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:[ARGlobalInterface findRecomentCacheURL]] ) {
        
        [self loadDataInCache];
    }
    
    
    UIView *footV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, FOOTVIEW_HEIGHT + 20)];
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(footV.width/2 -FOOTVIEW_WIDTH/2, 20, FOOTVIEW_WIDTH, FOOTVIEW_HEIGHT)];
    [footV addSubview:imageV];
    
    imageV.image = [UIImage imageNamed:@"museum_bottom_button@2x"];
    self.tableView.tableFooterView = footV;
    
    self.footV = imageV;

    
}


- (void) loadDataInCache
{
    NSData * fileData = [NSData dataWithContentsOfFile:[ARGlobalInterface findRecomentCacheURL]];
   
    
    
    NSDictionary * objects = [NSJSONSerialization JSONObjectWithData:fileData options:NSJSONReadingMutableContainers error:nil];
    if (!objects) {
        return;
    }
    
    NSDictionary *headDict = objects[@"data"][0];
    [self.headModel setValuesForKeysWithDictionary:headDict];
    
    NSArray *group = objects[@"data"];
    NSMutableArray *marray = [NSMutableArray array];
    
    for(int i = 1; i < group.count ; i ++)
    {
        NSDictionary *dict = group[i];
        ARFindChannels *channelModel = [[ARFindChannels alloc] init];
        [channelModel setValuesForKeysWithDictionary:dict];
        [marray addObject:channelModel];
    
    }
    self.channelsModels = marray;
    [self refrash];


}


- (void) refrash
{
    self.headView.headModel = self.headModel;
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (CGFloat)getTableViewHeight
{
    CGFloat y = 0;
    y += HEADVIEW_HEIGHT;
    
    for (ARFindChannels * channes in self.channelsModels) {
        y += channes.channels.count * CELL_HEIGHT;
        y += TABLEVIEW_FOOTER_HEIGHT + TABLEVIEW_HEADER_HEIGHT;
        
        
        
    }
    y += FOOTVIEW_HEIGHT + 20;
    return y - ([UIScreen mainScreen].bounds.size.height - TABBAR_HEIGHT - NAVAGATEBAR_HEIGHT - STARBAR_HEIGHT);

}

#pragma mark - scrollViewDelegate

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
 {

     
 if (scrollView.contentOffset.y  > 0) {
 self.headView.frame = CGRectMake(0, -scrollView.contentOffset.y, self.headView.width, self.headView.height);
 }
 else
 {
 self.headView.frame = CGRectMake(0, 0, self.headView.width, self.headView.height);
 }
     
     
     if (scrollView.contentOffset.y > [self getTableViewHeight]) {
         
         CGFloat  addHeight = scrollView.contentOffset.y  - [self getTableViewHeight];
         
         self.footV.frame = CGRectMake(self.footV.left, self.footV.top, self.footV.width, FOOTVIEW_HEIGHT + addHeight);
         
         
     }
     else
     {
         self.footV.frame = CGRectMake(self.footV.left, self.footV.top, self.footV.width, FOOTVIEW_HEIGHT);
     }
 
 }

#pragma mark - headViewDelegate
- (void)findVeiw:(ARFindHeardView *)headView WithIndex:(int)index
{
    
    
    NSLog(@" u chose %d",index);

}
 

#pragma mark - tableViewDelegate


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ARChannelModel *channel = [[self.channelsModels[indexPath.section] channels] objectAtIndex:indexPath.row];
    
    if (self.delegate) {
        [self.delegate findRecomentViewController:self WithChannel:channel];
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ARFindChannels *findChannels = self.channelsModels[section];
    
    
    return findChannels.channels.count;
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ARChannelModel *channel = [[self.channelsModels[indexPath.section] channels] objectAtIndex:indexPath.row];
    
    ChannelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChannelCellID"];
    
    cell.channel = channel;
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.channelsModels.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;

}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ARFindChannels *findChannels = self.channelsModels[section];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40 - 15 - 5, 150, 15)];
    [view addSubview:label];
    label.font = [UIFont systemFontOfSize:14];
    label.text = findChannels.title;
    label.textColor = [UIColor darkGrayColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [view addSubview:button];
    button.frame = CGRectMake(self.view.width - 50, 40 - 15 - 5, 30, 15);
    [button setTitle:@"更多" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    
    button.tag = section;
    [button addTarget:self action:@selector(onMoreButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    return view;
}


- (void)onMoreButtonClick:(UIButton *)sender
{
    NSString *index;
    NSString *title;
    NSString *cate;
    
    if (sender.tag == 0) {
        index = @"1011074";
        title = @"芝心小秘书";
        cate = @"5";
    }else if(sender.tag == 1)
    {
        index = @"758";
        title = @"用户推荐频道";
        cate = @"5";
    }
    
    NSDictionary *dict = @{@"index":index,@"title":title,@"cate":cate};
    

    [[NSNotificationCenter defaultCenter] postNotificationName:@"showMore" object:nil userInfo:dict];


   
    
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

@end
